/*
Version V20211124 TIMER2=125US INTERRUPT,�˰汾������ʹ��125US/625US�жϵ��û���

ע�����
1���˰汾�����ҪCS���ԣ������ϵ�5S���ټ���CS�����ź�
2���ϵ���Ҫ�ڵ�ѹ�������ȶ���������ȡ��׼ֵ�������ϵ�ȫ�Ժ��ٿ�ʼɨ��TK

history:

20211215:
	- TK����ֵ12bit��14bit
	
20211224:
	- CS����ʱ�ϵ����Ҫ��REVREINITCOUNTDOWNʱ������ٿ�ʼ������#define REVREINITCOUNTDOWN	5000/20��ʾ��Ҫ�ȴ�5���ٿ�ʼCS����

20220114:
	- �޸�kRevNoiseThreshold��REVREINITVAL����ֵ

20220412: 
	- ��Ҫ�޸�MULTI KEY,ֻ��CS ״̬��
	
20220629:
	- �Ż��������ϼ��Զ��ж�
	
20221212:
	- ����FREQ

*/					
//=============================================================================

//=============================================================================
//Name		:main.c
//Purpose	:main loop
//=============================================================================
#define		MAIN_C
#include "includeAll.h"

//=============================================================================

void InitialTimer2(void)
{
			T2CON = 0;

	//RCP2H = TH2 = (65536 - kTIMER2RLD) / 256; //0.125US timer2 interrupt
 //	RCP2L = TL2 = (65536 - kTIMER2RLD) % 256;
}

//=============================================================================
// Function:	void InitialIoport(void)
// Purpose:	initial io port
// Input:		none
// OutPut:	none
// Author:
//=============================================================================
void InitialIoport(void)
{
//	P0 = kP0PorStatus;	
	//P1 = kP1PorStatus;
	//P2 = kP2PorStatus;	
	//P3 = kP3PorStatus;	
	
	//P0OE = kP0MODE;			
	//P1MODL = kP1MODEL;	
	//P1MODH = kP1MODEH;	
	//P2MOD = kP2MODE;
	//P3MODL = kP3MODEL;
//	P3MODH = kP3MODEH;
}

//=============================================================================
// Function:	void ClearXRAM(void)
// Purpose:	initial ram
// Input:	none
// OutPut:	none
// Author:
//=============================================================================
void ClearXRAM(void)
{
	unsigned char xdata * data l_p_ucXdata;
	unsigned int idata i;
	l_p_ucXdata = 0xfd00;
	for(i = 0;i < 768;i++)
	{
		*(l_p_ucXdata++) = 0x00;	
	}
}
//=============================================================================
void main(void)
{	
	WDT_IS_STOP;
	CLKCON = kFSYSDIVVALUE;

	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	SELFCK = 1;
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	
	CLRWDT = 1;		//clear watchdog
	ClearXRAM();
	InitialIoport();
InitIoport();//�Լ�д
// �����ϵ��ʼ������ɾ��//////////////	
	InitTKreg();		// TK��ʼ��
	InitialTimer2();	// ��ʼ��TIMER2�����Ѿ�ռ��TIMER2�������޸�TIMER2������趨
	Pro_InitFREQ();		// �ϵ�ƽ�ⴥ�ذ���ȡ��FREQ���飬���ϵ羡�����ִ��ذ����޸�����������

	GetJMPnPn( );

	ADCHS =0x0b;	// ADC channel set VBG
 	INTFLG = 0xcf;
	g_ucStep = 0;
	g_uciSp = 161;
	


/* �û��õ�Timer0��Timer1��Timer3��UART�Լ�����ģ����Ҫ������հ״���ʼ�� */

//�û���ʼ������

/*------------------------------------------------------------------------*/
	//ET2 = 1; 			//timer2 interrupt enable
 //	TR2 = 1; 			//timer2 running
	//EA = 1;				//global interrupt enable
	
InitRegister();
	WDT_RUN_FAST_DIS_ILDE;
Buzz(25);

	while (1)
	{
		timerControl();
		getKey();


if(keyValue!=0)
{
	Buzz(25);
}
		if (longKeyHoldTime >= 8)
		{
			longKeyHoldTime = 0;
		}
		
		keyValue = 0;
	}
}

void timerControl()
{
	if (timer40ms >= D_40ms)
	{
		timer40ms = 0;
		if (longKeyValue == 0)
		{
			longKeyJudgeTime++;
		}
		else
		{
			longKeyHoldTime++;
		}
		
	}
	if (timer300ms >= D_300ms)
	{
		timer300ms = 0;
	}
	if (timer500ms >= D_500ms)
	{
		timer500ms = 0;
		b500ms = ~b500ms;
	}
	if (timer1s >= D_1s)
	{
		timer1s = 0;

		timer5s++;
		if (timer5s >= D_5s)
		{
			timer5s = 0;
			b5s = ~b5s;
		}

		timer1m++;
		if (timer1m >= D_1m)
		{
			timer1m = 0;
		}
	}
}

void getKey()
{

	static int16_t lastTkStatus = 0;
	uint8_t mask, LastkeyValue,
		i, j = 0, k = 0;

	if (!(AUX1 & 0x30))
		CLRWDT = 1; //	clear watchdog
	TKScan();		// 	???????????
	mask = 0x01;
	for (i = 0; i < TKCOUNT; i++)
	{
		if (((lastTkStatus & mask) == 0) && (g_wTKreg & mask))
		{
			j++;
			LastkeyValue = i + 1;
		}
		if (g_wTKreg & mask)
		{
			k++;
		}
		mask <<= 1;
	}
	if ((j == 1) && (k == 1))
	{
		keyValue = LastkeyValue;
		holdKey = keyValue;
	}
	else
	{
		LastkeyValue = 0;
		if (k > 1)
		{
			holdKey = 0;
		}

		keyValue = 0;
	}

	if ((lastTkStatus == g_wTKreg) && (g_wTKreg != 0))
	{
		    if (longKeyJudgeTime >= 25)
		{
			longKeyJudgeTime = 0;
			longKeyValue = holdKey;
		}
	}
	else
	{
		longKeyValue = 0;
		longKeyJudgeTime = 0;
	}
	lastTkStatus = g_wTKreg;
}
